package com.hc.fdkgoods.dao;


import com.hc.fdkgoods.db.tables.records.MapSellerSkuSupplierRecord;
import com.hc.fdkgoods.vo.vo_9.ForwarderSkuListVO;
import com.hc.fdkgoods.vo.vo_9.ForwarderSkuListVO_9;
import org.jooq.*;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

import static com.hc.fdkgoods.db.Tables.FORWARDER_SUPPLIER;
import static com.hc.fdkgoods.db.Tables.MAP_SELLER_SKU_SUPPLIER;

@Repository
public class AssignSupplierDao extends BaseDao {
    public int insertIntoOrUpdate(ForwarderSkuListVO_9 inputForwarderItem) {

        for (ForwarderSkuListVO skuItem : inputForwarderItem.getForwarderSkuMapDTOList()) {
//            Integer idx = 0;
            boolean idx;
            // select spu id
            SelectConditionStep<Record1<Long>> querySpuId = db.select(MAP_SELLER_SKU_SUPPLIER.SELLER_SPU_ID).from(MAP_SELLER_SKU_SUPPLIER).
                    where(MAP_SELLER_SKU_SUPPLIER.SELLER_SPU_ID.eq(skuItem.getSellerSpuId()));
            // select when sku id exists
            if (skuItem.getSellerSkuId() != null) {
//                querySpuId.and(MAP_SELLER_SKU_SUPPLIER.SELLER_SKU_ID.eq(skuItem.getSellerSkuId()));
                idx = db.fetchExists(
                        db.select(MAP_SELLER_SKU_SUPPLIER.SELLER_SPU_ID)
                                .from(MAP_SELLER_SKU_SUPPLIER)
                                .where(MAP_SELLER_SKU_SUPPLIER.SELLER_SPU_ID.eq(skuItem.getSellerSpuId()))
                                .and(MAP_SELLER_SKU_SUPPLIER.SELLER_SKU_ID.eq(skuItem.getSellerSkuId())));
            } else {
                idx = db.fetchExists(
                        db.select(MAP_SELLER_SKU_SUPPLIER.SELLER_SPU_ID)
                                .from(MAP_SELLER_SKU_SUPPLIER)
                                .where(MAP_SELLER_SKU_SUPPLIER.SELLER_SPU_ID.eq(skuItem.getSellerSpuId())));
            }
//            idx = db.fetchExists(
//                    db.select(MAP_SELLER_SKU_SUPPLIER.SELLER_SPU_ID)
//                            .from(MAP_SELLER_SKU_SUPPLIER)
//                            .where(MAP_SELLER_SKU_SUPPLIER.SELLER_SPU_ID.eq(skuItem.getSellerSpuId())));
            Integer curSupplierId = db.select(FORWARDER_SUPPLIER.FORWARDER_SUPPLIER_ID).from(FORWARDER_SUPPLIER)
                    .where(FORWARDER_SUPPLIER.CITY.eq(skuItem.getCityName())
                            .and(FORWARDER_SUPPLIER.MARKET.eq(skuItem.getMarketName())
                                    .and(FORWARDER_SUPPLIER.FLOOR.eq(skuItem.getFloorName())))
                            .and(FORWARDER_SUPPLIER.BUSINESS.eq(skuItem.getBusinessName())))
                    .fetchAnyInto(Integer.class);
            System.out.println("############  " + curSupplierId);


            if (idx) {
                System.out.println("update: " + idx);
                UpdateSetMoreStep<MapSellerSkuSupplierRecord> updateMSSS = db.update(MAP_SELLER_SKU_SUPPLIER)
                        .set(MAP_SELLER_SKU_SUPPLIER.SUPPLIER_ID, Long.valueOf(curSupplierId))
                        .set(MAP_SELLER_SKU_SUPPLIER.ITEM_NO, skuItem.getItemNo())
                        .set(MAP_SELLER_SKU_SUPPLIER.CITY, skuItem.getCityName())
                        .set(MAP_SELLER_SKU_SUPPLIER.FLOOR, skuItem.getFloorName())
                        .set(MAP_SELLER_SKU_SUPPLIER.MARKET, skuItem.getMarketName())
                        .set(MAP_SELLER_SKU_SUPPLIER.BUSINESS, skuItem.getBusinessName())
                        .set(MAP_SELLER_SKU_SUPPLIER.PURCHASE_PRICE, skuItem.getPurchasePrice());
                if (skuItem.getSupplierName() != null) {
                    updateMSSS.set(MAP_SELLER_SKU_SUPPLIER.SUPPLIER_NAME, skuItem.getSupplierName());
                } else {
                    String supplierName = skuItem.getCityName() + " " + skuItem.getMarketName() + " " + skuItem.getFloorName() + " " + skuItem.getBusinessName();
                    updateMSSS.set(MAP_SELLER_SKU_SUPPLIER.SUPPLIER_NAME, supplierName);
                }
                if (skuItem.getRemark() != null) {
                    updateMSSS.set(MAP_SELLER_SKU_SUPPLIER.REMARK, skuItem.getRemark());
                }
                if (skuItem.getSellerSkuId() != null) {
                    updateMSSS.set(MAP_SELLER_SKU_SUPPLIER.SELLER_SKU_ID, skuItem.getSellerSkuId())
                            .where(MAP_SELLER_SKU_SUPPLIER.SELLER_SKU_ID.eq(skuItem.getSellerSkuId()))
                            .and(MAP_SELLER_SKU_SUPPLIER.SELLER_SPU_ID.eq(skuItem.getSellerSpuId()));
                } else {
                    updateMSSS.where(MAP_SELLER_SKU_SUPPLIER.SELLER_SPU_ID.eq(skuItem.getSellerSpuId()));
                }
                updateMSSS.execute();

            } else {
                System.out.println("insert: " + idx);
                System.out.println(skuItem);
                InsertSetMoreStep<MapSellerSkuSupplierRecord> insertMSSS = db.insertInto(MAP_SELLER_SKU_SUPPLIER)
                        .set(MAP_SELLER_SKU_SUPPLIER.SUPPLIER_ID, Long.valueOf(curSupplierId))
                        .set(MAP_SELLER_SKU_SUPPLIER.SELLER_SPU_ID, skuItem.getSellerSpuId())
                        .set(MAP_SELLER_SKU_SUPPLIER.ITEM_NO, skuItem.getItemNo())
                        .set(MAP_SELLER_SKU_SUPPLIER.CITY, skuItem.getCityName())
                        .set(MAP_SELLER_SKU_SUPPLIER.FLOOR, skuItem.getFloorName())
                        .set(MAP_SELLER_SKU_SUPPLIER.MARKET, skuItem.getMarketName())
                        .set(MAP_SELLER_SKU_SUPPLIER.BUSINESS, skuItem.getBusinessName())
                        .set(MAP_SELLER_SKU_SUPPLIER.PURCHASE_PRICE, skuItem.getPurchasePrice());
                if (skuItem.getSupplierName() != null) {
                    insertMSSS.set(MAP_SELLER_SKU_SUPPLIER.SUPPLIER_NAME, skuItem.getSupplierName());
                } else {
                    String supplierName = skuItem.getCityName() + " " + skuItem.getMarketName() + " " + skuItem.getFloorName() + " " + skuItem.getBusinessName();
                    insertMSSS.set(MAP_SELLER_SKU_SUPPLIER.SUPPLIER_NAME, supplierName);
                }
                if (skuItem.getRemark() != null) {
                    insertMSSS.set(MAP_SELLER_SKU_SUPPLIER.REMARK, skuItem.getRemark());
                }
                if (skuItem.getSellerSkuId() != null) {
                    insertMSSS.set(MAP_SELLER_SKU_SUPPLIER.SELLER_SKU_ID, skuItem.getSellerSkuId());
                }
                insertMSSS.execute();
            }
        }


        return 0;
    }
}
